//
//  UserPreferences.m
//  Baidu
//
//  Created by C_HAO on 15/4/7.
//  Copyright (c) 2015年 C_HAO. All rights reserved.
//

#import "UserPreferences.h"

@implementation UserPreferences

- (id)init
{
    self = [super init];
    if (self) {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSUserDomainMask, YES);
        NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"translation/"];
        NSString *fileConfigPath = [path stringByAppendingPathComponent:@"_config"];
        NSDictionary *dictionary = [NSKeyedUnarchiver unarchiveObjectWithFile:fileConfigPath];
        if (dictionary) {
            _baidu = [[dictionary objectForKey:@"BaiDu"] boolValue];
            _youdao = [[dictionary objectForKey:@"YouDao"] boolValue];
        }else {
            _baidu = YES;
            _youdao = YES;
        }
    }
    return self;
}


- (void)setBaidu:(BOOL)baidu
{
    _baidu = baidu;
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSUserDomainMask, YES);
    NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"translation/"];
    NSString *fileConfigPath = [path stringByAppendingPathComponent:@"_config"];
    NSMutableDictionary *dictionary = [NSKeyedUnarchiver unarchiveObjectWithData:[[NSMutableData alloc] initWithContentsOfFile:fileConfigPath]];
    if (!dictionary) dictionary = [[NSMutableDictionary alloc] init];
    [dictionary setValue:[NSNumber numberWithBool:_baidu] forKey:@"BaiDu"];
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:dictionary];
    [data writeToFile:fileConfigPath atomically:YES];
}

- (void)setYoudao:(BOOL)youdao
{
    _youdao = youdao;
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSUserDomainMask, YES);
    NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"translation/"];
    NSString *fileConfigPath = [path stringByAppendingPathComponent:@"_config"];
    NSMutableDictionary *dictionary = [NSKeyedUnarchiver unarchiveObjectWithData:[[NSMutableData alloc] initWithContentsOfFile:fileConfigPath]];
    if (!dictionary) dictionary = [[NSMutableDictionary alloc] init];
    [dictionary setValue:[NSNumber numberWithBool:_youdao] forKey:@"YouDao"];
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:dictionary];
    [data writeToFile:fileConfigPath atomically:YES];
}



@end
